# -*- coding:utf-8-*-
from app import db
import hashlib


class Tender(db.Model):
    __table_name__ = "tender"
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    url = db.Column(db.String(256), nullable=False)
    title = db.Column(db.String(100))
    content = db.Column(db.Text())
    release_id = db.Column(db.Integer, db.ForeignKey('release.id'))
    agent_release_id = db.Column(db.Integer, db.ForeignKey('agent.id'))
    attachment_id = db.Column(db.Integer, db.ForeignKey('attachment.id'))
    content_label = db.Column(db.Text())
    release_time = db.Column(db.DateTime())
    deadline = db.Column(db.DateTime())
    types = db.Column(db.String(256))
    status = db.Column(db.Integer, nullable=False)
    bidder_id = db.Column(db.Integer, db.ForeignKey("bidder.id"))
    create_time = db.Column(db.DateTime())


class Release(db.Model):
    __table_name__ = "release"
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    company_name = db.Column(db.String(256), nullable=False)
    principal = db.Column(db.String(30), nullable=False)
    contact_number = db.Column(db.String(30))
    address = db.Column(db.String(256))
    position = db.Column(db.Integer, db.ForeignKey('position.id'))


class Agent(db.Model):
    __table_name__ = "agent"
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    company_name = db.Column(db.String(256), nullable=False)
    principal = db.Column(db.String(30), nullable=False)
    contact_number = db.Column(db.String(30))
    address = db.Column(db.String(256))
    position = db.Column(db.Integer, db.ForeignKey('position.id'))


class Attachment(db.Model):
    __table_name__ = "attachment"
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    file_type = db.Column(db.String(10))
    file_url = db.Column(db.String(256))
    member_access = db.Column(db.Integer)


class Bidder(db.Model):
    __table_name__ = "bidder"
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    company_name = db.Column(db.String(256), nullable=False)
    position = db.Column(db.Integer, db.ForeignKey('position.id'))


class Position(db.Model):
    __table_name__ = "position"
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    province = db.Column(db.String(30))
    city = db.Column(db.String(30))
    county = db.Column(db.String(100))


class Member(db.Model):
    __table_name__ = "member"
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    member_name = db.Column(db.String(128), nullable=False)
    password = db.Column(db.String(256), nullable=False)

    def __init__(self, member_name, password):
        self.member_name = member_name
        self.password= hashlib.md5(password)


class MemberInfo(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    member_id = db.Column(db.Integer, db.ForeignKey('member.id'))
    company_name = db.Column(db.String(256))
    principal = db.Column(db.String(30))
    email = db.Column(db.String(64))
    phone = db.Column(db.String(20))
    post = db.Column(db.String(20))
    position = db.Column(db.Integer, db.ForeignKey('position.id'))